# Maintainer: David Runge <dvzrv@archlinux.org>

_name=JUCE
pkgbase=juce
pkgname=(juce juce-docs)
pkgver=7.0.2
pkgrel=1
pkgdesc="Cross-platform C++ framework"
arch=(loong64 x86_64)
url="https://github.com/juce-framework/juce/"
license=(Apache BSD GPL3 ISC)
makedepends=(alsa-lib cmake freetype2 doxygen flac gendesk graphviz gtk3 jack
ladspa libjpeg-turbo libpng libvorbis python webkit2gtk zlib)
options=(debug)
source=(
  $pkgname-$pkgver.tar.gz::https://github.com/juce-framework/$pkgname/archive/$pkgver.tar.gz
  $pkgname-6.1.2-cmake_install.patch
  $pkgname-6.1.2-cmake_juce_utils.patch
  $pkgname-6.1.3-cmake_link_against_system_deps.patch
  $pkgname-6.1.2-devendor_libs.patch
  $pkgname-6.1.2-fst_vst.patch
  $pkgname-6.1.2-projucer_disable_update_check.patch
  $pkgname-6.1.2-projucer_juce_paths.patch
  ${pkgname}_VSTInterface.h
)
sha512sums=('0d81b8b68784268b0b8b23bb79c98a2b0bde6770272a2aa6e33a2f22a208e48601423f4fad9477137de925a97c93dc29b7ae5b8466a7a7687027c72e50195c6e'
            '52aba043ad3e5154b67f4d140f1d1e936962325763b47bbec57e1ef0a9cccbe6fa0b99598a756cb76853b5c1c5084b32055fc6a76f992b10323d867c82023399'
            '91ef9ced3a72db3b24d5892149cb3ac19fd132a0cc48a36afe1664864e7deb4928a3a19a92aee11f277beccbb8e500e4fa1aba74d77a52b3517ce4bb429904c6'
            '8d5105e7ea0977cba598c13e53708ca88cdea52c3fc92cca8849eb4e64209d4902d8eef6a1bda0894138f2e1fe4247156b1d39e74207d978534768f92490f2e8'
            'ccec528a1daaf255a4686dad2d59314adab03a935a79893b04a8f04256179a2b1ba84fa563fd6781b314b9d2fc1ddf4892576f77d748e40577747013b3a60bc3'
            'dddf5510f9391fc663db29174215e9575e9ec3956bb649c8a83564b0dcfa1202cc430fadd8faafa390b45c3a2e79ba75e3cdfb118db1e5c35836fad954b67e32'
            '2e1b823d9e5611e2c9efa1459f309f9add76d163411bb32cbd8c93f9751c6b97842af430f866f9ebce02d7602a655fd1f9d62f0530e5dc4d2621cbbd5444aad8'
            '766b761bf60a09fd01e9e6422df5b34c5305d41672b82b2c54819c581a952af5d7c8c39868c8615b57b14f772624bae4545ec878f2098ab5b8afb2ecf59146a1'
            '36a244740872cd203db3e1cebba5a5e245a3300a8c6c76046455ecc38083348f069987d0cba8f569390b4f0ba855799b8ff559cb540f02382b67a46d5b9cc420')
b2sums=('624282c0e324078be4fa8cbaaca8e3cc09642bec74432fdf92dd9115a234c849cbdc0519c874aaf15f8727c8196985ccf6c9cf6ba0574743a6cf35fd050970ac'
        '2419fd707b03a5e33cef03576cbeddb99ab4fd67f4fbec8047668ffb1daf78803cd608d7255893394e6d83dce5afd51608793384567228130a0082bcc9a67f60'
        '8accc2bb2def2b6c91084879071299c0d94e652dad61f15457d8a3c49507db30dcbd25f0311cc044661470ff986d7566ef503ec183b6ada535341dcb456d8e6d'
        'cc766ff15858ed1d66c4f79b182903931e9bcfb0c83ab7ef5fccb487185b9ea31284764c232df962b1438671d79710f9a8d5a0e1d36ffeea43fca23b446c80e8'
        'ee72102ecdf57490e9aec0fe6368d61e3c9f86a8b13fd35b3889eac591d716cc98c19b87d6d09360848202b4a108bbc39639d36e28d69908c80ee514b2cf40ee'
        'b0a8758f6a97a487b7734839d59f0c0684d69ac67acba168a707296eeb82fb13c630506dcb05f45e1eaf9620adc881c6c467fdc3f3cfbb0326a4e6ebf36407e8'
        'cd6c59deed414529c49dbf8506905cc7848bb48073e054d4cf2450c17c7268860330bd2d2e6d30e5a2a68cec2106e3498619e85ce602439f75ba9393084498f6'
        '4d42e73b4d15f0ef16a55d927de43127518f4420fe54f78b2c2a0e57a08eb192138f65f381017d0f1e6ca286009f63f16c269b58e7405c35894c0bc3ac91de7b'
        'f56e59984bde66d482fdeae3cefc0d4dc9e25c9c170f2f0b46ec53bd63ec72432b1764ff282ef8093c1029a427852c09087827d22023b526786a03977cbb7a32')

prepare() {
  gendesk \
    -n \
    --pkgname "com.juce_projucer" \
    --name "Projucer" \
    --pkgdesc "Cross-platform project manager and C++ code editor" \
    --exec "Projucer" \
    --icon "Projucer" \
    --genericname "Code manager and editor" \
    --categories "Development"

  cd $_name-$pkgver
  for _patch in ../*.patch; do
    printf "Applying patch %s\n" "${_patch}"
    patch -Np1 -i "${_patch}"
  done

  # remove bundled sources for flac, libogg, vorbis, libjpeg, libpng and zlib
  # as we are linking against system libs
  rm -rvf modules/juce_audio_formats/codecs/flac/ \
    modules/juce_audio_formats/codecs/oggvorbis/ \
    modules/juce_audio_plugin_client/AU/ \
    modules/juce_graphics/image_formats/jpglib/ \
    modules/juce_graphics/image_formats/pnglib/ \
    modules/juce_core/zip/zlib/
}

build() {
  # enable GPL mode via CPPFLAGS
  export CPPFLAGS+=" -DJUCER_ENABLE_GPL_MODE=1"

  cmake -DCMAKE_INSTALL_PREFIX=/usr \
        -DCMAKE_BUILD_TYPE=None \
        -DJUCE_BUILD_EXTRAS=ON \
        -DJUCE_TOOL_INSTALL_DIR=bin \
        -Wno-dev \
        -B build \
        -S $_name-$pkgver
  make VERBOSE=1 -C build
  make -C $_name-$pkgver/docs/doxygen
}

package_juce() {
  depends=(gcc-libs glibc hicolor-icon-theme libpng libfreetype.so libjpeg.so
  ttf-font zlib)
  optdepends=(
    'fst: for building FST plugins'
    'python: for building LV2 plugins'
  )

  make DESTDIR="$pkgdir" VERBOSE=1 -C build install
  # projucer has no install target
  install -vDm 755 build/extras/Projucer/Projucer_artefacts/None/Projucer -t "$pkgdir/usr/bin/"
  # install custom vst2 handling from juce < 5.4.1
  install -vDm 644 juce_VSTInterface.h -t "$pkgdir/usr/share/juce/modules/juce_audio_processors/format_types/"
  # xdg desktop integration
  install -vDm 644 *.desktop -t "$pkgdir/usr/share/applications/"
  install -vDm 644 $_name-$pkgver/examples/Assets/juce_icon.png "$pkgdir/usr/share/icons/hicolor/512x512/apps/Projucer.png"
  # docs
  install -vDm 644 $_name-$pkgver/{{BREAKING-CHANGES,ChangeList}.txt,README.md} -t "$pkgdir/usr/share/doc/$pkgname/"
  # license
  install -vDm 644 $_name-$pkgver/LICENSE.md -t "$pkgdir/usr/share/licenses/$pkgname/"
}

package_juce-docs() {
  (
    cd $_name-$pkgver/docs/doxygen/doc
    find . -type f -exec install -vDm 644 {} "$pkgdir/usr/share/doc/$pkgbase/html/"{} \;
  )
  # license
  install -vDm 644 $_name-$pkgver/LICENSE.md -t "$pkgdir/usr/share/licenses/$pkgname/"
}
